<!--  -->
<template>
  <div>
    <el-dialog
      title="中压停电事件新增"
      :visible.sync="show"
      v-if="show"
      width="90%"
      :close-on-click-modal="false"
      :before-close="handleClose"
      top="1vh"
    >
        <div :style="'height:'+screenHeight+'px;'">
            <el-form ref="addForm" :model="addForm" :rules="addRules" label-width="140px" class="addForm">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="数据范围:">
                            <el-date-picker size="mini" type="date" v-model="sjfw" format="yyyy 年 MM 月" value-format="yyyy-MM-dd" disabled></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="16" class="right">
                        <el-form-item>
                            <el-button size="mini" @click="handleClose">关闭</el-button>
                            <el-button size="mini" type="primary" @click="handleSave">保存</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="责任部门:" prop="zrbmmc">
                            <el-row>
                                <el-col :span="16">
                                    <el-input size="mini" v-model="addForm.zrbmmc" disabled></el-input>
                                </el-col>
                                <el-col :span="8" class="selectZrbm">
                                    <el-button size="mini" type="primary" @click="selectZrbm">选择</el-button>
                                </el-col>
                            </el-row>
                        </el-form-item>
                        <el-form-item label="停电设备:" prop="tdsbm">
                            <el-row>
                                <el-col :span="16">
                                    <el-select
                                        ref="tdsbRef"
                                        v-model="addForm.tdsbm"
                                        filterable
                                        remote
                                        reserve-keyword
                                        placeholder="请选择"
                                        size="mini"
                                        :clearable="true"
                                        :remote-method="remoteMethodTdsb"
                                        :loading="tdsbloading"
                                        @change="getCheckValueTdsb"
                                        :disabled="addForm.tdzrm == ''"
                                    >
                                        <el-option v-for="item in tdsbOptions" :key="item.value" :label="item.label" :value="item.value">
                                            <span style="float: left">{{ item.label }}</span>
                                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                        </el-option>
                                    </el-select>
                                </el-col>
                                <el-col :span="8" class="selectTdsb">
                                    <el-button size="mini" type="primary" @click="selectTdsb" :disabled="addForm.tdzrm == ''">选择</el-button>
                                </el-col>
                            </el-row>
                        </el-form-item>
                        <el-form-item label="起始时间:" prop="qssj">
                            <el-date-picker size="mini" type="datetime" v-model="addForm.qssj" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" :picker-options="pickerQssj" @change="handleQssj"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="详细说明:">
                            <el-input size="mini" v-model="qsm" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="责任原因:" prop="tdzrm">
                            <el-row>
                                <el-col :span="16">
                                    <el-select
                                        ref="zryyRef"
                                        v-model="addForm.tdzrm"
                                        filterable
                                        remote
                                        reserve-keyword
                                        placeholder="请选择"
                                        size="mini"
                                        :clearable="false"
                                        :remote-method="remoteMethodZryy"
                                        :loading="zryyloading"
                                        @change="getCheckValueZryy"
                                    >
                                        <el-option v-for="item in zryyOptions" :key="item.value" :label="item.label" :value="item.value">
                                            <span style="float: left">{{ item.label }}</span>
                                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                        </el-option>
                                    </el-select>
                                </el-col>
                                <el-col :span="8" class="selectZryy">
                                    <el-button size="mini" type="primary" @click="selectZryy">选择</el-button>
                                </el-col>
                            </el-row>
                        </el-form-item>
                        <el-form-item label="技术原因:" prop="tdjsm">
                            <el-select size="mini" v-model="addForm.tdjsm" placeholder="请选择" :disabled="addForm.tdsbm == ''" clearable @change="tdjsChange">
                                <el-option v-for="item in tdjsOptions" :key="item.value" :label="item.text" :value="item.value">
                                    <span style="float: left">{{ item.text }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="终止时间:" prop="zzsj">
                            <el-date-picker size="mini" type="datetime" v-model="addForm.zzsj" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" :picker-options="pickerZzsj" @change="handleZzsj"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="停电时间同步:">
                            <el-checkbox v-model="checked" @change="changeSftb">同步线段和用户</el-checkbox>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="停电性质:" prop="tdxzm">
                            <el-select size="mini" v-model="addForm.tdxzm" placeholder="请选择" :disabled="tdxzDisabled" @change="tdxzmChange">
                                <el-option v-for="item in tdxzOptions" :key="item.value" :label="item.text" :value="item.value">
                                <span class="txt">{{ item.text }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="特殊分析:" prop="fxbm">
                            <el-select size="mini" v-model="addForm.fxbm" placeholder="请选择" @change="fxbmChange">
                                <el-option v-for="item in tsfxOptions" :key="item.value" :label="item.text" :value="item.value">
                                <span class="txt">{{ item.text }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="持续时间:">
                            <el-input size="mini" v-model="addForm.cxsj" disabled>
                                <template slot="append">小时</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="备注:">
                            <el-input size="mini" v-model="addForm.comm"></el-input>
                        </el-form-item> 
                    </el-col>
                </el-row>
            </el-form>

            <div class="table">
                <div class="tableOperate">
                    <el-row>
                        <el-col :span="6" class="left"><span>停电范围</span></el-col>
                        <el-col :span="18" class="right">
                            <el-button size="mini" type="primary" @click="handleAddXd">添加</el-button>
                            <el-button size="mini" type="primary" @click="cccEvent('show', yxzyTdxdColumnTable)">列配置</el-button>
                        </el-col>
                    </el-row>
                </div>

                <div class="tableSj">
                    <el-table :data="tableData" ref="tableData" border style="width: 100%;" height="100%" v-loading="loading" @selection-change="handleSelectionChange">
                        <el-table-column
                            type="selection"
                            fixed="left"
                            align="center"
                            width="55">
                        </el-table-column>
                        <el-table-column label="序号" type="index" width="50" align="center" fixed="left"></el-table-column>
                        <el-table-column align="center" prop="tdhs" label="停电用户" width="80" fixed="left">
                            <template slot-scope="scope">
                                <span>{{scope.row.tdhs}}</span>
                                <el-button type="text" size="mini" @click="handleYHClickSee(scope.row)">(查看)</el-button>
                            </template>
                        </el-table-column>
                        <template v-for="col in yxzyTdxdColumnData">
                            <el-table-column
                                :show-overflow-tooltip="true"
                                :key="col.id"
                                :prop="col.field"
                                :label="col.title"
                                :min-width="col.minWidth"
                                :align="col.align"
                                :header-align="col.align"
                                :sortable="col.sortable"
                                :formatter="col.formatter"
                                :fixed="col.fixed"
                            ></el-table-column>
                        </template>
                        <el-table-column align="center" fixed="right" label="操作" width="150">
                            <template slot-scope="scope">
                                <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
                                <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                
                <div class="pagination">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="tdxdTempForm.current" :page-size="tdxdTempForm.size" :page-sizes="[10, 20, 40, 80]" layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
    </el-dialog>

    <!-- 选择停电责任码All -->
    <GetZryybmAll ref="getZryybmAllRef" @handleGetZryyAll="handleGetZryyAll"></GetZryybmAll>

    <!-- 选择设备编码 -->
    <GetTdsbbm ref="getTdsbbmRef" @handleGetTdsb="handleGetTdsb"></GetTdsbbm>

    <!-- 选择责任部门编码 -->
    <GetDwbm :treeObj="treeObj" ref="getDwbmRef" @handleGetDwbm="handleGetDwbm"></GetDwbm>

    <!-- 线段新增 -->
    <yxzyXdAdd :xdPardata="xdPardata" @handleAddYxxd="handleAddYxxd"></yxzyXdAdd>

    <!-- 查看用户 -->
    <yxzyYhAdd :yhPardata="yhPardata" @handleAddYxyh="handleAddYxyh"></yxzyYhAdd>

    <!-- 列配置 -->
    <ccc ref="ccc"></ccc>
  </div>
</template>

<script>
import { dataModulePrefix } from '../../../../config';
import { saveYxzySjTemp, commitYxzySjTemp, cleanYxzySjTemp } from "@/api/sjwh/yxzysj.js";
import { getYxzyXdTempListPage, deleteYxzyXdTemp } from "@/api/sjwh/yxzyxd.js";
import { getTdxzbmByBzm } from '@/api/dmgl/tdxz.js';
import { getZryybmList } from '@/api/dmgl/zryy.js';
import { getTdsbbmList } from '@/api/dmgl/sbbm.js';
import { getJsyybmGyTdsb } from '@/api/dmgl/jsyy.js';
import { getDictCombo } from '@/api/tools/dict.js';
import GetZryybmAll from "@/dialogCommom/GetZryybmAll"; // 责任原因编码弹窗
import GetTdsbbm from "@/dialogCommom/GetTdsbbm"; // 停电设备编码弹窗
import GetDwbm from "@/dialogCommom/GetDwbm"; // 单位编码弹窗
import { dateFormatter, getBetweenHour_b } from "@/utils/util.js";
import { checkSpecialKey } from '@/utils/validate.js';
import { getUserInfo } from "@/utils/utilInfo.js";
import ccc from "@/dialogCommom/ccc";
import yxzyXdAdd from './yxzyXdAdd.vue'; // 线段新增弹窗
import yxzyYhAdd from './yxzyYhAdd.vue'; // 查看用户弹窗
export default {
    data() {
        const that = this
        const checkTdsb = (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                let tdzrlx = that.addForm.tdzrm.slice(0, 2)
                if (tdzrlx == '51') {
                    callback('请选择停电设备')
                } else {
                    callback()
                }
            }
        }
        const checkComm = (rule, value, callback) => {
            if (value) {
                if (checkSpecialKey(value)) {
                    callback()
                } else {
                    callback('禁止输入`~!#$%^&*等特殊字符')
                }
            } else {
                callback()
            }
        }
        return {
            show: false,
            screenHeight: 0, // 弹框高度
            token: '', // 唯一标识符
            czy: getUserInfo.userName, // 操作员
            sftb: '0', // 是否同步，1是/0否
            addForm: {
                dwbm: '', // 录入单位编码
                dwmc: '', // 录入单位名称
                dwxz: '', // 录入单位性质
                dwxzm: '', // 录入单位性质码
                zrbm: '', // 责任部门编码
                zrbmmc: '', // 责任部门名称
                tdzrm: '', // 停电责任码
                tdzrmc: '', // 停电责任名称
                zrtjbzm: '', // 责任统计分类标志
                tdxz: '', // 停电性质
                tdxzm: '', // 停电性质码      
                tdsbm: '', // 停电设备码
                tdsbmc: '', // 停电设备名称
                tdjsm: '', // 停电技术码
                tdjsmc: '', // 停电技术名称
                fxbm: '', // 特殊分析编码
                fxmc: '', // 特殊分析名称
                qssj: '', // 停电起始时间
                zzsj: '', // 停电终止时间
                cxsj: '0', // 持续时间
                qsm: '', // 详细说明
                comm: '', // 备注
                id: '' // 事件id
            },
            addRules: {
                zrbmmc: [
                    { required: true, message: '请选择责任部门', trigger: 'blur' }
                ],
                tdzrm: [
                    { required: true, message: '请选择责任原因', trigger: 'blur' }
                ],
                qssj: [
                    { required: true, message: '请选择起始时间', trigger: 'blur' }
                ],
                zzsj: [
                    { required: true, message: '请选择终止时间', trigger: 'blur' }
                ],
                tdxzm: [
                    { required: true, message: '请选择停电性质', trigger: 'blur' }
                ],
                tdsbm: [
                    { validator: checkTdsb, trigger: 'blur' }
                ],
                tdjsm: [
                    { required: true, message: '请选择技术原因', trigger: 'blur' }
                ],
                fxbm: [
                    { required: true, message: '请选择特殊分析', trigger: 'blur' }
                ],
                comm: [
                    { validator: checkComm, trigger: 'blur' }
                ]
            },
            pickerQssj: {
                disabledDate: (time) => {
                    if (this.addForm.zzsj) {
                        return time.getTime() > new Date(this.addForm.zzsj)
                    }
                    if (this.addForm.qssj) {
                        return time.getTime() > Date.now()
                    }
                }
            },
            pickerZzsj: {
                disabledDate: (time) => {
                    if (this.addForm.qssj) {
                        return time.getTime() > Date.now()
                    }
                }
            },
            checked: false, // 停电时间同步
            tsfxDict: 'dict_tsfx', // 特殊分析字典分类值
            tsfxOptions: [], // 特殊分析下拉框数据
            tdxzOptions: [], // 停电性质下拉框数据
            tdjsOptions: [], // 技术原因下拉框数据

            zryyloading: false, // 责任原因模糊搜索时的加载动画
            zryyOptions: [], // 责任原因下拉框数据
            zryylist: [], // 责任原因列表
            zryyData: [], // 责任原因请求接口返回值

            tdsbloading: false, // 停电设备模糊搜索时加载动画
            tdsbOptions: [], // 停电设备下拉框数据
            tdsblist: [], // 停电设备列表
            tdsbData: [], // 停电设备请求接口返回值

            treeObj: { // 传给选择责任部门编码组件的值
                type: 'radio',
                dwbm: ''
            },

            tdxdTempForm: { // 获取中压停电线段临时列表参数
                sjId: '', // 事件id
                current: 1,
                size: 10
            },
            total: 0, // 中压停电线段数据总条数
            tableData: [], // 中压停电线段表格数据
            yxzyTdxdColumnTable: 'yx_zytdxd', // 中压停电线段列配置参数
            yxzyTdxdColumnData: [], //中压停电线段表格列数据
            multipleSelection: [], // 选中的数据
            loading: false, // 中压停电线段表格加载动画

            xdPardata: { //传给添加线段弹窗组件的值
                TOKEN: '',
                czy: '',
                dwbm: '',
                visibal: false,
                list_xd_show: [],
                qssj: '',
                zzsj: '',
                sjId: ''
            },

            yhPardata: { // 传给查看用户弹窗组件的值
                TOKEN: '',
                sjId: '',
                czy: '',
                visibal: false,
                xdRow: {}
            }
        }
    },

    components: {
        GetZryybmAll,
        GetTdsbbm,
        GetDwbm,
        yxzyXdAdd,
        yxzyYhAdd,
        ccc
    },

    computed: {
        sjfw() {
            const that = this
            let year = that.addForm.qssj.split('-')[0]
            let month = that.addForm.qssj.split('-')[1]
            month = parseInt(month, 10)
            return year + '-' + month
        },
        tdxzDisabled() {
            const that = this
            if (that.addForm.zrbm) {
                if (that.tdxzOptions.length > 1) {
                    return false
                } else {
                    return true
                }
            } else {
                return true
            }
        },
        qsm() {
            const that = this
            let qsm  = ''
            let arr  = [that.addForm.tdsbmc, that.addForm.tdjsmc, that.addForm.tdzrmc]
            arr.forEach(item => {
                if (item) {
                    qsm = qsm + item + '，'
                }
            })
            return qsm.substring(0, qsm.length-1)
        }
    },

    props: {
        propAddData: {
            type: Object,
            default: function() {
                return {};
            }
        }
    },

    watch: {
        propAddData: {
            deep: true,
            immediate: true,
            handler(newVal, oldVal) {
                if (newVal.show) {
                    const that = this
                    that.show = newVal.show
                    that.token = new Date().getTime() + ''
                    that.getZryyList() // 获取责任原因所有数据
                    that.getTdsbList() // 获取停电设备所有数据
                    that.addForm.qssj = that.getCurrentMonthFirst()
                    let treeNode = JSON.parse(sessionStorage.getItem('treeNode'))
                    that.addForm.dwbm = treeNode.dwbm
                    that.addForm.dwmc = treeNode.dwmc
                    that.addForm.dwxz = treeNode.dwxz
                    that.addForm.dwxzm = treeNode.dwxzm
                    that.addForm.zrbm = newVal.zrbm
                    that.addForm.zrbmmc = newVal.zrbmmc
                }
            }
        }
    },

    beforeUpdate () { 
        //解决列配置引发的滚动条问题
        const that = this
        that.$nextTick(()=>{
            that.$refs.tableData.doLayout()
        })
    },

    mounted() {
        const that = this
        that.cccEvent("load", that.yxzyTdxdColumnTable)
        getDictCombo({typeCode: that.tsfxDict}).then(res => {
            if (res.code == 0) {
                that.tsfxOptions = res.data
            } else {
                that.$message.error(res.message)
            }
        })
        that.screenHeight = (document.documentElement.clientHeight || document.body.clientHeight) * 0.9
        window.addEventListener('resize', that.onResize)
    },

    methods: {
        /** 关闭弹窗 */
        handleClose() {
            const that = this
            if (that.addForm.id) {
                let vo = {
                    TOKEN: that.token,
                    czy: that.czy,
                    id: that.addForm.id
                }
                cleanYxzySjTemp(vo).then(res => {
                    if (res.code == 0) {
                        that.cleanYxzySjFormTemp()
                    } else {
                        that.$message.error(res.message)
                    }
                })
            } else {
                that.cleanYxzySjFormTemp()
            }
        },

        /** 清空表单数据 */
        cleanYxzySjFormTemp() {
            const that = this
            that.show = false
            that.token = ''
            that.czy = getUserInfo.userName
            that.sftb = '0'
            that.addForm = {
                dwbm: '',
                dwmc: '',
                zrbm: '',
                zrbmmc: '',
                tdzrm: '',
                tdzrmc: '',
                zrtjbzm: '',
                tdxz: '',
                tdxzm: '',      
                tdsbm: '',
                tdsbmc: '',
                tdjsm: '',
                tdjsmc: '',
                fxbm: '',
                fxmc: '',
                qssj: '',
                zzsj: '',
                cxsj: '0',
                qsm: '',
                comm: '',
                id: ''
            }
            that.checked = false
            that.treeObj = {
                type: 'radio',
                dwbm: ''
            }
            that.tdxdTempForm = {
                sjId: '',
                current: 1,
                size: 10
            }
            that.total = 0
            that.tableData = []
            that.xdPardata = {
                TOKEN: '',
                czy: '',
                dwbm: '',
                visibal: false,
                list_xd_show: [],
                qssj: '',
                zzsj: '',
                sjId: ''
            }
        },

        /** 保存 */
        handleSave() {
            const that = this
            that.$refs['addForm'].validate(async valid => {
                if (valid) {
                    if (that.tableData.length < 1) {
                        that.$message.warning('请添加停电范围')
                        return false
                    }
                    that.addForm.qsm = that.qsm
                    let vo = {
                        TOKEN: that.token,
                        czy: that.czy,
                        sftb: that.sftb,
                        tdsj: that.addForm
                    }
                    let res = await commitYxzySjTemp(vo)
                    if (res.code == 0 && res.data.status) {
                        that.$message.success('中压停电事件新增成功')
                        that.$emit('updateYxzySjTable')
                        that.cleanYxzySjFormTemp()
                    } else {
                        that.$message.error(res.message)
                    }      
                }
            })
        },

        /** 起始时间选择 */
        handleQssj() {
            const that = this
            let nowTime = dateFormatter(new Date(), 8, false, false)
            let timecxsj = getBetweenHour_b(that.addForm.qssj, nowTime)
            if (timecxsj < 0) {
                that.$message.error("起始时间不能大于当前时间")
                return false
            }
            if (that.addForm.zzsj) {
                that.getQs2Zz('qssj', () => {
                    that.updateTdsjTimeTemp()
                })
            }
        },

        /** 终止时间选择 */
        handleZzsj() {
            const that = this
            let nowTime = dateFormatter(new Date(), 8, false, false)
            let timecxsj = getBetweenHour_b(that.addForm.zzsj, nowTime)
            if (timecxsj < 0) {
                that.$message.error("终止时间不能大于当前时间")
            }
            if (that.addForm.qssj) {
                that.getQs2Zz('zzsj', () => {
                    that.updateTdsjTimeTemp()
                }) 
            }
        },

        /**起始时间、终止时间比较 */
        getQs2Zz(str, callback) {
            const that = this
            let qs = that.addForm.qssj
            let zz = that.addForm.zzsj
            let cxxs = getBetweenHour_b(qs, zz)
            if (cxxs <= 0) {
                that.$message.warning('终止时间不能小于或等于起始时间,请重新选择')
                that.addForm.cxsj = 0
                if (str == 'qssj') {
                    that.addForm.qssj = ''
                } else {
                    that.addForm.zzsj = ''
                }
            } else {
                that.addForm.cxsj = cxxs
                callback && callback()
            }
            if (Number(cxxs) > 12) {
                that.$message.warning("请注意，持续时间大于12小时")
            }
        },

        /** 起始时间、终止时间选择后保存更新 */
        updateTdsjTimeTemp() {
            const that = this
            if (that.addForm.id) {
                that.saveYxSjTemp(() => {
                    that.getTdxdList()
                })
            }
        },


        /* 选择责任部门编码 */
        selectZrbm() {
            const that = this
            that.treeObj.dwbm = that.addForm.dwbm
            that.$refs.getDwbmRef.openDwbm()
        },

        /* 责任部门编码组件回传 */
        handleGetDwbm(val) {
            const that = this
            that.addForm.zrbmmc = val.data.dwmc
            that.addForm.zrbm = val.data.dwbm
        },

        /**获取责任原因数据 */
        getZryyList() {
            const that = this
            getZryybmList().then((res) => {
                let newData = []
                if (res.code === 0) {
                    that.zryyData = res.data
                    res.data.forEach(item => {
                        let obj = {
                            value: item.tdzrm,
                            label: item.tdzryy
                        }
                        newData.push(obj)
                    })

                    that.zryylist = newData.map((item) => {
                        return {
                            value: `${item.value}`,
                            label: `${item.label}`,
                            search: `${item.value}-${item.label}`
                        }
                    })
                } else {
                    that.$message.error(res.message)
                }
            })
        },
        /** 责任原因模糊查询 */
        remoteMethodZryy(query) {
            const that = this
            if (query != '') {
                that.zryyloading = true
                setTimeout(() => {
                that.zryyloading = false
                    that.zryyOptions = that.zryylist.filter((item) => {
                        return item.search.toLowerCase().indexOf(query.trim().toLowerCase()) > -1
                    })
                }, 200)
            } else {
                that.zryyOptions = []
            }
        },
        /** 责任原因下拉框选择  */
        getCheckValueZryy(val) {
            const that = this
            let filObj = this.zryyData.filter((item) => item.tdzrm === val)
            that.addForm.tdzrmc = filObj[0].tdzryy
            that.addForm.zrtjbzm = filObj[0].tjbzm
            that.getTdxzbmByBzm(filObj[0].bzm)
        },
        /** 责任原因编码弹窗  */
        selectZryy() {
            this.$refs.getZryybmAllRef.openZryybm()
        },
        /**责任原因编码弹窗回传 */
        handleGetZryyAll(val){
            const that = this
            that.zryyOptions = that.zryylist
            that.addForm.tdzrm = val.tdzrm
            that.addForm.tdzrmc = val.tdzryy
            that.addForm.zrtjbzm = val.tjbzm
            that.getTdxzbmByBzm(val.bzm)
        },
        /** 根据责任原因BZM查询停电性质 */
        getTdxzbmByBzm(bzms) {
            const that = this
            getTdxzbmByBzm([bzms]).then(res => {
                if (res.code == 0) {
                    let arr = []
                    res.data.forEach(item => {
                        let obj = {
                            value: item.tdxzm,
                            text: item.tdxz
                        }
                        arr.push(obj)
                    })
                    that.tdxzOptions = arr
                    if (res.data.length == 1) {
                        that.addForm.tdxzm = res.data[0].tdxzm
                        that.addForm.tdxz = res.data[0].tdxz
                    }
                } else {
                    that.$message.error(res.message)
                }
            })
        },
            
        /** 停电设备模糊查询 */
        remoteMethodTdsb(query) {
            const that = this
            if (query !== "") {
                that.tdsbloading = true
                setTimeout(() => {
                    that.tdsbloading = false
                    that.tdsbOptions = that.tdsblist.filter((item) => {
                        return item.search.toLowerCase().indexOf(query.trim().toLowerCase()) > -1
                    })
                }, 200)
            } else {
                that.tdsbOptions = []
            }
        },
        /**获取停电设备数据 */
        getTdsbList() {
            getTdsbbmList().then((res) => {
                const that = this
                let newData = []
                if (res.code === 0) {
                    that.tdsbData = res.data

                    res.data.forEach(item => {
                        let obj = {
                            value: item.tdsbm,
                            label: item.tdsbmc
                        }
                        newData.push(obj)
                    })

                    that.tdsblist = newData.map((item) => {
                        return {
                            value: `${item.value}`,
                            label: `${item.label}`,
                            search: `${item.value}-${item.label}`
                        }
                    })
                }
            })
        },
        /** 停电设备下拉框选择  */
        getCheckValueTdsb(val) {
            const that = this
            let filObj = this.tdsbData.filter((item) => item.tdsbm === val)
            that.addForm.tdsbmc = filObj[0].tdsbmc
            that.getJsyybmGyTdsb(filObj[0].tdsbm)
        },
        /** 停电设备编码弹窗 */
        selectTdsb() {
            this.$refs.getTdsbbmRef.openTdsbbm()
        },
        /** 停电设备编码弹窗回传 */
        handleGetTdsb(val){
            const that = this
            that.tdsbOptions = that.tdsblist
            that.addForm.tdsbm = val.tdsbm
            that.addForm.tdsbmc = val.tdsbmc
            that.getJsyybmGyTdsb(val.tdsbm)
        },
        /** 根据设备编码查询设备技术原因 */
        getJsyybmGyTdsb(tdsbm) {
            const that = this
            getJsyybmGyTdsb(tdsbm).then(res => {
                if (res.code == 0) {
                    let arr = []
                    res.data.forEach(item => {
                        let obj = {
                            value: item.tdjsm,
                            text: item.tdjsyy
                        }
                        arr.push(obj)
                    })
                    that.tdjsOptions = arr
                } else {
                    that.$message.error(res.message)
                }
            })
        },

        /* 选择技术原因 */
        tdjsChange(val) {
            const that = this
            that.tdjsOptions.forEach(item => {
                if (item.value == val) {
                    that.addForm.tdjsmc = item.text
                }
            })
        },

        /* 停电性质原因 */
        tdxzmChange(val) {
            const that = this
            that.tdxzOptions.forEach(item => {
                if (item.value == val) {
                    that.addForm.tdxz = item.text
                }
            })
        },

        /** 特殊分析选择 */
        fxbmChange(val) {
            const that = this
            that.tsfxOptions.forEach(item => {
                if (item.value == val) {
                    that.addForm.fxmc = item.text
                }
            })
        },

        /** 是否同步线段和用户 */
        changeSftb() {
            const that = this
            if (that.checked) {
                that.sftb = '1'
            } else {
                that.sftb = '0'
            }
            if (that.addForm.qssj && that.addForm.zzsj) {
                that.updateTdsjTimeTemp()
            }
        },

        /* 获取当前月份的第一天 */
        getCurrentMonthFirst(){
            let date = new Date()
            date.setDate(1)
            let month = parseInt(date.getMonth()+1)
            let day = date.getDate()
            if (month < 10) {
                month = '0' + month
            }
            if (day < 10) {
                day = '0' + day
            }
            return date.getFullYear() + '-' + month + '-' + day + ' 00:00'
        },

        // 选择中压停电线段
        handleSelectionChange(val) {
            this.multipleSelection = val
        },

        /* 获取中压停电线段临时列表 */
        async getTdxdList() {
            const that = this
            that.loading = true
            let res = await getYxzyXdTempListPage(that.tdxdTempForm)
            if (res.code == 0) {
                that.tableData = res.data.records
                that.total = res.data.total
            } else {
                that.$message.error(res.message)
            }
            that.loading =  false
        },

        /* 更改每页数据条数 */
        handleSizeChange(val) {
            const that = this
            that.tdxdTempForm.size = val
            that.getTdxdList()
        },

        /* 更改页码 */
        handleCurrentChange(val) {
            const that = this
            that.tdxdTempForm.current = val
            that.getTdxdList()
        },

        /* 添加中压停电临时线段 */
        handleAddXd() {
            const that = this
            if (!that.addForm.qssj || !that.addForm.zzsj) {
                this.$message.warning("请选择起始时间 或者 终止时间")
                return false
            }
            if (that.addForm.id) {
                that.openXdDialog()
            } else {
                that.saveYxSjTemp(() => {
                    that.openXdDialog()
                })
            }
        },

        /** 保存中压停电临时事件 */
        async saveYxSjTemp(callback) {
            const that = this
            that.addForm.qsm = that.qsm
            let vo = {
                TOKEN: that.token,
                czy: that.czy,
                sftb: that.sftb,
                tdsj: that.addForm
            }
            let res = await saveYxzySjTemp(vo)
            if (res.code == 0 && res.data.status) {
                that.tdxdTempForm.sjId = res.data.record.id
                that.addForm.id = res.data.record.id
                if (callback) {
                    callback && callback()
                }
            } else {
                that.$message.error(res.message)
            }
        },

        /** 打开线段新增弹窗 */
        openXdDialog() {
            const that = this
            let xdPardata = {
                TOKEN: that.token,
                visibal: true,
                dwbm: that.addForm.zrbm,
                list_xd_show: that.tableData,
                qssj: that.addForm.qssj,
                zzsj: that.addForm.zzsj,
                sjId: that.addForm.id,
                czy: that.czy,
            };
            that.xdPardata = JSON.parse(JSON.stringify(xdPardata))
        },
        /** 添加线段回传参数 */
        handleAddYxxd(val) {
            if (val === true) {
                this.getTdxdList()
            }
        },
        
        /* 修改中压停电临时线段 */
        handleEditXd(row) {

        },

        /* 删除中压停电临时线段 */
        handleDeleteXd(row) {
            const that = this
            that.$confirm('此操作将删除该临时线段, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let vo = {
                    TOKEN: that.token,
                    czy: that.czy,
                    sjId: that.addForm.id,
                    xdIds: [row.id]
                }
                deleteYxzyXdTemp(vo).then(res => {
                    if (res.code == 0 && res.data) {
                        that.$message.success('删除成功')
                        that.getTdxdList()
                    } else {
                        that.$message.error(res.message)
                    }
                })
            }).catch(() => {
                console.log('已取消删除')          
            })
        },

        /** 查看用户 */
        handleYHClickSee(row) {
            const that = this
            let xdRow = JSON.parse(JSON.stringify(row))
            let yhPardata = {
                TOKEN: that.token,
                sjId: that.addForm.id,
                visibal: true,
                xdRow: [xdRow],
                czy: that.czy,
            };
            that.yhPardata = JSON.parse(JSON.stringify(yhPardata))
        },
        /** 查看用户【修改】回传 */
        handleAddYxyh(val) {
            if (val.bool === true) {
                this.getTdxdList()
            }
        },

        //列配置
        cccEvent(method, table) {
            if (method === "show") {
                this.$refs.ccc.show(table);
            } else if (method === "load") {
                new Promise((resolve, reject) => {
                    this.$nextTick(() => {
                        resolve(this.$refs.ccc.load(table));
                    });
                }).then(response => {
                    this.yxzyTdxdColumnData = response;
                });
            } else if (method === "export") {
                let vo = {
                    tableCode: table,
                    param: this.tdxdTempForm,
                    url: `${dataModulePrefix}/yx-zytdxd-temp/listByPage`,
                    method: "POST",
                    fileName: '中压停电临时线段',
                    total: this.total
                };
                this.$refs.ccc.exportExcel(vo);
            }
        },

        /* 屏幕缩放 */
        onResize() {
            this.screenHeight = (document.documentElement.clientHeight || document.body.clientHeight) * 0.9
        }
    }
};
</script>
<style lang='scss' scoped>
.addForm /deep/ .el-form-item {
    margin-bottom: 20px;
}
.addForm /deep/ .el-form-item__label {
    line-height: 30px;
}
.addForm /deep/ .el-form-item__content {
    line-height: 30px;
}
.addForm /deep/ .el-date-editor.el-input {
    width: 100%;
}
.addForm /deep/ .el-select {
    width: 100%;
}
/deep/ .el-tree > .el-tree-node {
  min-width: 100%;
  display: inline-block;
}
/deep/ .el-dialog__footer{
  padding: 5px 10px 5px;
}
/deep/ .el-dialog__header{
  padding: 4px 10px 4px;
  line-height: 30px;
}
/deep/ .el-dialog__headerbtn{
  top: 10px;
}
/deep/ .el-dialog__body {
    padding: 10px 20px 1px;
}
.selectZrbm {
  padding-left: 8px;
}
.selectZrbm /deep/ .el-button--primary {
  width: 100%;
}
.selectZryy {
  padding-left: 8px;
}
.selectZryy /deep/ .el-button--primary {
  width: 100%;
}
.selectTdsb {
  padding-left: 8px;
}
.selectTdsb /deep/ .el-button--primary {
  width: 100%;
}
.right {
    text-align: right;
}
.table {
    border-top: solid 1px rgb(240, 240, 240);
    height: calc(100% - 255px);
}
.table .tableOperate {
    padding: 5px 0 1px;
}
.tableOperate .left span {
    display: inline-block;
    height: 28px;
    line-height: 28px;
}
.pagination {
    text-align: center;
}
.tableSj {
    height: calc(100% - 68px);
}
</style>
